package model;

import java.io.File;

public class FilesFinder {

     /**.
     * all the possible video files extensions
     */
	private static String videos_ext[]={"3g2", "3gp", "3gp2", "3gpp", "60d", "ajp", "asf",
            "asx", "avchd", "avi", "bik", "bix", "box", "cam",
            "divx", "dmf", "dv", "dvr-ms", "evo", "flc",
            "fli", "flic", "flv", "flx", "gvi", "gvp", "h264",
            "m1v", "m2p", "m2ts", "m2v", "m4e", "m4v", "mjp",
            "mjpeg", "mjpg", "mkv", "moov", "mov", "movhd",
            "movie", "movx", "mp4", "mpe", "mpeg", "mpg", "mpv",
            "mpv2", "mxf", "nsv", "nut", "ogg", "ogm", "omf",
            "ps", "qt", "ram", "rm", "rmvb", "swf", "ts", "vfw",
            "vid", "video", "viv", "vivo", "vob", "vro", "wm",
            "wmv", "wmx", "wrap", "wvx", "wx", "x264", "xvid"};

	/***.
	 * Find all the video files stored in a folder
     * @param path, path of the folder
     * @return the movie list
	 */
	public static MoviesList findMovies(final MoviesList list, String path) {
		File file = new File(path);
		MoviesList listMovie = new MoviesList();
		boolean exist = false;
		listMovie=MoviesInfosFinder.getMoviesInfos(browseFolders(listMovie,file));

		if (list != null) {
			for (int i = 0; i < listMovie.getMovies().size(); i++) {
				for (int j = 0; j < list.getMovies().size(); j++) {
					if(list.getMovies().get(j).getSearch().equals(listMovie.getMovies().get(i).getSearch())){
						if(!list.getMovies().get(j).getPath().equals(listMovie.getMovies().get(i).getPath())){
							list.getMovies().get(j).setPath(listMovie.getMovies().get(i).getPath());
						}
						exist = true;
					}
				}
				if (!exist) {
					list.getMovies().add(listMovie.getMovies().get(i));
					exist = false;
				}
			}
			return list;
		}
		return listMovie;
	}

	/***.
	 * Browse inside the folder recursively
	 * @param file, current file
	 * @return the movie list
	 */
	private static MoviesList browseFolders(MoviesList listMovie, File file) {
		File[] list = file.listFiles();
		for (int i = 0; i < list.length; i++) {
			if (list[i].isFile()) {
				if (checkMovieFormat(list[i])) {
					listMovie.addMovies(new Movie(list[i].getAbsolutePath(),clearMovieTitle(list[i].getName().replaceFirst("[.][^.]+$", ""))));
				}
			} else {
				listMovie = browseFolders(listMovie, list[i]);
			}
		}
		return listMovie;

	}

	/***.
	 * Check the extension of the file
	 * @param file
     * @return true if it is a video file, else false 
	 */
	public static boolean checkMovieFormat(final File file) {

		String[] split = file.getName().split("\\.");
	    String ext = split[split.length - 1];

	    for (int i = 0; i < videos_ext.length; i++) {
	    	if (ext.equals(videos_ext[i])) {
	    		return true;
	    	}
	    }
		return false;
	}

	/**.
	 * This function is used to clear the movie title
	 * @param title
	 * @return the clean title
	 */
	public static String clearMovieTitle(String title) {
		String clearTitle = null;
		boolean isOK = true;

		clearTitle = title.replaceAll("[\\.\\-\\_\\s\\[\\]\\(\\)]", "+").replaceAll("\\+\\+", "+");

		String[] str = clearTitle.split("\\+");

		if (str.length != 0) {
			clearTitle = str[0];
		}
		for (int i = 1; i < str.length; i++) {
			if (str[i].matches("-?\\d+")&&Integer.parseInt(str[i])>1900) {
				isOK = false;
			} else if (str[i].contains("DVD") || str[i].contains("BD") || str[i].contains("TS") || str[i].contains("TV") || str[i].contains("cd")) {
				isOK = false;
			}
			if (isOK) {
				clearTitle = clearTitle + "+" + str[i];
			}
		}
		return clearTitle.toLowerCase();
	}

}
